package com.spring.api.web.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.stereotype.Service;

import com.spera.duanxin.SxUtil;
import com.spring.api.web.dao.ApiMapper;
import com.spring.api.web.dao.SmsSendHistoryMapper;
import com.spring.api.web.model.SmsSendHistory;
import com.spring.api.web.service.ApiService;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.web.iputil.HttpCUtils;
import com.spring.security.web.model.BaseResult;

@Service
public class ApiServiceImpl implements ApiService{
	
	@Resource
	private ApiMapper apiMapper;
	
	@Resource
	private SmsSendHistoryMapper smsSendHistoryMapper;

	@Override
	public BaseResult selectCooTaskDailyByUser(String userId) {
		BaseResult ret = new BaseResult();
		List<Map<String,Object>> retL = apiMapper.selectCooTaskDailyByUser(userId);
		ret.setDataset(retL).setMsg("查询成功");
		return ret;
	}

	@Override
	public BaseResult sendDcSms(String phone, String content) {
		return null;
	}

	@Override
	public BaseResult sendYxSms(String phone, String content) {
		BaseResult ret = new BaseResult();
		if(StringUtils.isBlank(phone)||StringUtils.isBlank(content)){
			ret.setMsg("手机号或内容不可为空").setStatus("0");
		}
		String retXml = "";
//		Map<String,Object> parm = new HashMap<String,Object>();
		Map<String,String> parm = new HashMap<String,String>();
		parm.put("account", SxUtil.qf_username);
		parm.put("password", SxUtil.qf_password);
		parm.put("mobile", phone);
		parm.put("content", content+",回T退订【蒂芭卜】");
		System.out.println("sendYxSms-------->"+phone);
		try {
//			retXml = HttpClientUtils.httpPost("http://api.yx.ihuyi.com/webservice/sms.php?method=Submit", parm);
			retXml = HttpCUtils.postParameters("http://api.yx.ihuyi.com/webservice/sms.php?method=Submit", parm);
			Document doc = DocumentHelper.parseText(retXml); 
			Element root = doc.getRootElement();

			String code = root.elementText("code");
			String msg = root.elementText("msg");
			String smsid = root.elementText("smsid");
			//保存日志
			saveSmsLog(phone,content,smsid,2,code,msg);
			if("2".equals(code)){
				ret.setMsg("发送成功");
			}else{
				ret.setMsg("发送失败"+msg).setStatus("0");
			}
		} catch (Exception e) {
			e.printStackTrace();
			ret.setMsg("发送失败").setStatus("0");
			saveSmsLog(phone,content,"",2,"9999","短信接口调用异常："+e.getMessage());
		}
		return ret;
	}
	
	//保存短信下行日志
	public void saveSmsLog(String phones,String content,String smsid,int type,String smssendresult,String msg){
		List<SmsSendHistory> smsList = new ArrayList<SmsSendHistory>();
		if(ApplicationUtils.isNotNull(content)&&ApplicationUtils.isNotNull(phones)){
			String[] phoneArr = phones.split(",");
			for(String phone:phoneArr){
				SmsSendHistory sms = new SmsSendHistory();
				sms.initInfo();
				sms.setId(ApplicationUtils.randomUUID()).setContent(content).setEstimatedsendtime(new Date())
				.setSendtime(new Date())
				.setSmstype(type).setSmssendresult(Integer.parseInt(smssendresult)).setMobile(phone)
				.setSmsstatus(smssendresult.equals("2")?1:2).setSmsid(smsid).setMsg(msg);
				smsList.add(sms);
			}
		}
		smsSendHistoryMapper.insertByList(smsList);
	}

}
